//
//  HMHomeTableViewCell.swift
//  Black22
//
//  Created by Apple on 16/11/22.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

/*
    1. 原创微博视图
    2. 转发微博视图
    3. toolbar视图
 */

//  自定义首页cell
class HMHomeTableViewCell: UITableViewCell {
    
    //  记录toolbar顶部约束
    var toolBarTopConstraint: Constraint?
    
    //  视图所对应的viewModel
    var statusViewModel: HMStatusViewModel? {
        didSet {
            originalView.statusViewModel = statusViewModel
            retweetView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            
            //  先卸载约束
            toolBarTopConstraint?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil {
                //  表示有转发微博
                retweetView.isHidden = false
                toolBar.snp_updateConstraints(closure: { (make) in
                    //  记录修改后的约束
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                //  表示没有转发微博
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }
            
            
            
        }
    }
    
    
    // MARK:    --懒加载
    //  原创微博视图
    fileprivate lazy var originalView: HMStatusOriginalView = HMStatusOriginalView()
    //  转发微博视图
    fileprivate lazy var retweetView: HMStatusRetweetView = HMStatusRetweetView()
    //  toolBar
    fileprivate lazy var toolBar: HMStatusToolBar = HMStatusToolBar()
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    
    private func setupUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.left.equalTo(contentView)
            make.width.equalTo(ScreenWidth)
            
        }
        
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
            
        }
        
        toolBar.snp_makeConstraints { (make) in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(retweetView)
            make.trailing.equalTo(retweetView)
            make.height.equalTo(35)
            //  设置约束，让toolbar底部约束等于contentview的底部
            make.bottom.equalTo(contentView)
        }
        

        
        
//        //  设置关键约束
//        contentView.snp_makeConstraints { (make) in
//            make.bottom.equalTo(toolBar)
//            make.leading.equalTo(self)
//            make.trailing.equalTo(self)
//            make.top.equalTo(self)
//        }
        
    }

}
